//
//  CollectionViewManager.m
//  rn
//
//  Created by 牛冬冬 on 2018/3/28.
//  Copyright © 2018年 Jevons-niu. All rights reserved.
//

#import "TestCollectionViewManager.h"
#import "TestCollectionView.h"

#import <React/RCTBridge.h>             //进行通信的头文件
#import <React/RCTEventDispatcher.h>    ///<事件派发，不导入会引起Xcode警告

@interface TestCollectionViewManager ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation TestCollectionViewManager

//  事件的导出，onClickBanner对应view中扩展的属性
RCT_EXPORT_VIEW_PROPERTY(onClickBanner, RCTBubblingEventBlock)

//  通过宏RCT_EXPORT_VIEW_PROPERTY完成属性的映射和导出
RCT_EXPORT_VIEW_PROPERTY(autoScrollTimeInterval, CGFloat);

RCT_EXPORT_VIEW_PROPERTY(imageURLStringsGroup, NSArray);

RCT_EXPORT_VIEW_PROPERTY(autoScroll, BOOL);

- (UIView *)view
{
    UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
    TestCollectionView *collectionView = [[TestCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    return collectionView;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(TestCollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (!collectionView.onClickBanner) {
        return;
    }
    
    logd(@"index: %@ \n oc did click %li", indexPath, [collectionView.reactTag integerValue]);
    
    //  导出事件
    collectionView.onClickBanner(@{
                                   @"target": collectionView.reactTag,
                                   @"section": [NSNumber numberWithInteger:indexPath.section],
                                   @"row": [NSNumber numberWithInteger:indexPath.row],
                                  });
}

- (NSInteger)numberOfSectionsInCollectionView:(TestCollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(TestCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(TestCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    return cell;
}

@end
